const express = require('express');
const validate = require('../../middleware/validate');
const { auth, authorize } = require('../../middleware/auth');
const nutritionValidation = require('../../validators/nutrition.validator');
const nutritionController = require('../../controllers/nutrition.controller');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Nutrition
 *   description: 营养分析管理 (管理员接口)
 */

// 营养目标路由
router
  .route('/goals')
  /**
   * @swagger
   * /admin/nutrition/goals:
   *   post:
   *     summary: 创建用户营养目标
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - userId
   *               - caloriesGoal
   *               - nutritionGoals
   *               - macroRatio
   *             properties:
   *               userId:
   *                 type: string
   *                 description: 用户ID
   *               caloriesGoal:
   *                 type: number
   *                 description: 卡路里目标
   *               nutritionGoals:
   *                 type: object
   *                 properties:
   *                   protein:
   *                     type: number
   *                     description: 蛋白质目标(g)
   *                   fat:
   *                     type: number
   *                     description: 脂肪目标(g)
   *                   carbohydrate:
   *                     type: number
   *                     description: 碳水化合物目标(g)
   *                   fiber:
   *                     type: number
   *                     description: 膳食纤维目标(g)
   *                   sugar:
   *                     type: number
   *                     description: 糖目标(g)
   *                   sodium:
   *                     type: number
   *                     description: 钠目标(mg)
   *               macroRatio:
   *                 type: object
   *                 properties:
   *                   protein:
   *                     type: number
   *                     description: 蛋白质比例(%)
   *                   fat:
   *                     type: number
   *                     description: 脂肪比例(%)
   *                   carbohydrate:
   *                     type: number
   *                     description: 碳水化合物比例(%)
   *     responses:
   *       "201":
   *         description: 创建成功
   *       "400":
   *         description: 参数错误或用户已有营养目标
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .post(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.createNutritionGoal),
    nutritionController.createNutritionGoal
  );

router
  .route('/goals/:userId')
  /**
   * @swagger
   * /admin/nutrition/goals/{userId}:
   *   get:
   *     summary: 获取用户营养目标
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: userId
   *         required: true
   *         schema:
   *           type: string
   *         description: 用户ID
   *     responses:
   *       "200":
   *         description: 获取成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到用户营养目标
   */
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionGoal),
    nutritionController.getNutritionGoal
  )
  /**
   * @swagger
   * /admin/nutrition/goals/{userId}:
   *   put:
   *     summary: 更新用户营养目标
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: userId
   *         required: true
   *         schema:
   *           type: string
   *         description: 用户ID
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             properties:
   *               caloriesGoal:
   *                 type: number
   *                 description: 卡路里目标
   *               nutritionGoals:
   *                 type: object
   *                 properties:
   *                   protein:
   *                     type: number
   *                     description: 蛋白质目标(g)
   *                   fat:
   *                     type: number
   *                     description: 脂肪目标(g)
   *                   carbohydrate:
   *                     type: number
   *                     description: 碳水化合物目标(g)
   *                   fiber:
   *                     type: number
   *                     description: 膳食纤维目标(g)
   *                   sugar:
   *                     type: number
   *                     description: 糖目标(g)
   *                   sodium:
   *                     type: number
   *                     description: 钠目标(mg)
   *               macroRatio:
   *                 type: object
   *                 properties:
   *                   protein:
   *                     type: number
   *                     description: 蛋白质比例(%)
   *                   fat:
   *                     type: number
   *                     description: 脂肪比例(%)
   *                   carbohydrate:
   *                     type: number
   *                     description: 碳水化合物比例(%)
   *     responses:
   *       "200":
   *         description: 更新成功
   *       "400":
   *         description: 参数错误
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到用户营养目标
   */
  .put(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.updateNutritionGoal),
    nutritionController.updateNutritionGoal
  )
  /**
   * @swagger
   * /admin/nutrition/goals/{userId}:
   *   delete:
   *     summary: 删除用户营养目标
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: userId
   *         required: true
   *         schema:
   *           type: string
   *         description: 用户ID
   *     responses:
   *       "200":
   *         description: 删除成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到用户营养目标
   */
  .delete(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionGoal),
    nutritionController.deleteNutritionGoal
  );

// 营养建议路由
router
  .route('/suggestions')
  /**
   * @swagger
   * /admin/nutrition/suggestions:
   *   post:
   *     summary: 创建营养建议
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             required:
   *               - userId
   *               - type
   *               - content
   *               - relevantFoods
   *             properties:
   *               userId:
   *                 type: string
   *                 description: 用户ID
   *               type:
   *                 type: string
   *                 enum: [营养均衡, 热量控制, 蛋白质补充, 碳水适当, 脂肪控制, 膳食纤维, 维生素补充, 钠摄入控制, 其他]
   *                 description: 建议类型
   *               content:
   *                 type: string
   *                 description: 建议内容
   *               relevantFoods:
   *                 type: array
   *                 items:
   *                   type: object
   *                   properties:
   *                     foodId:
   *                       type: string
   *                       description: 食物ID
   *                     reason:
   *                       type: string
   *                       description: 推荐理由
   *     responses:
   *       "201":
   *         description: 创建成功
   *       "400":
   *         description: 参数错误
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .post(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.createNutritionSuggestion),
    nutritionController.createNutritionSuggestion
  )
  /**
   * @swagger
   * /admin/nutrition/suggestions:
   *   get:
   *     summary: 获取营养建议列表
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: query
   *         name: userId
   *         schema:
   *           type: string
   *         description: 用户ID
   *       - in: query
   *         name: type
   *         schema:
   *           type: string
   *           enum: [营养均衡, 热量控制, 蛋白质补充, 碳水适当, 脂肪控制, 膳食纤维, 维生素补充, 钠摄入控制, 其他]
   *         description: 建议类型
   *       - in: query
   *         name: isRead
   *         schema:
   *           type: boolean
   *         description: 是否已读
   *       - in: query
   *         name: startDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 开始日期
   *       - in: query
   *         name: endDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 结束日期
   *       - in: query
   *         name: sortBy
   *         schema:
   *           type: string
   *         description: 排序方式 (例如 createdAt:desc)
   *       - in: query
   *         name: limit
   *         schema:
   *           type: integer
   *           minimum: 1
   *         default: 10
   *         description: 每页数量
   *       - in: query
   *         name: page
   *         schema:
   *           type: integer
   *           minimum: 1
   *         default: 1
   *         description: 页码
   *     responses:
   *       "200":
   *         description: 获取成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionSuggestions),
    nutritionController.getNutritionSuggestions
  );

router
  .route('/suggestions/:id')
  /**
   * @swagger
   * /admin/nutrition/suggestions/{id}:
   *   get:
   *     summary: 获取单条营养建议
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 营养建议ID
   *     responses:
   *       "200":
   *         description: 获取成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到营养建议
   */
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionSuggestion),
    nutritionController.getNutritionSuggestion
  )
  /**
   * @swagger
   * /admin/nutrition/suggestions/{id}:
   *   put:
   *     summary: 更新营养建议
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 营养建议ID
   *     requestBody:
   *       required: true
   *       content:
   *         application/json:
   *           schema:
   *             type: object
   *             properties:
   *               type:
   *                 type: string
   *                 enum: [营养均衡, 热量控制, 蛋白质补充, 碳水适当, 脂肪控制, 膳食纤维, 维生素补充, 钠摄入控制, 其他]
   *                 description: 建议类型
   *               content:
   *                 type: string
   *                 description: 建议内容
   *               isRead:
   *                 type: boolean
   *                 description: 是否已读
   *               relevantFoods:
   *                 type: array
   *                 items:
   *                   type: object
   *                   properties:
   *                     foodId:
   *                       type: string
   *                       description: 食物ID
   *                     reason:
   *                       type: string
   *                       description: 推荐理由
   *     responses:
   *       "200":
   *         description: 更新成功
   *       "400":
   *         description: 参数错误
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到营养建议
   */
  .put(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.updateNutritionSuggestion),
    nutritionController.updateNutritionSuggestion
  )
  /**
   * @swagger
   * /admin/nutrition/suggestions/{id}:
   *   delete:
   *     summary: 删除营养建议
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: id
   *         required: true
   *         schema:
   *           type: string
   *         description: 营养建议ID
   *     responses:
   *       "200":
   *         description: 删除成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   *       "404":
   *         description: 未找到营养建议
   */
  .delete(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.deleteNutritionSuggestion),
    nutritionController.deleteNutritionSuggestion
  );

// 标记所有建议为已读
router
  .route('/suggestions/read-all/:userId')
  /**
   * @swagger
   * /admin/nutrition/suggestions/read-all/{userId}:
   *   post:
   *     summary: 标记用户所有营养建议为已读
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: path
   *         name: userId
   *         required: true
   *         schema:
   *           type: string
   *         description: 用户ID
   *     responses:
   *       "200":
   *         description: 操作成功
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .post(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionGoal),
    nutritionController.markAllSuggestionsAsRead
  );

// 营养分析路由
router
  .route('/analysis')
  /**
   * @swagger
   * /admin/nutrition/analysis:
   *   get:
   *     summary: 获取用户营养分析数据
   *     tags: [Nutrition]
   *     security:
   *       - bearerAuth: []
   *     parameters:
   *       - in: query
   *         name: userId
   *         schema:
   *           type: string
   *         required: true
   *         description: 用户ID
   *       - in: query
   *         name: startDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 开始日期 (不提供则根据period自动计算)
   *       - in: query
   *         name: endDate
   *         schema:
   *           type: string
   *           format: date
   *         description: 结束日期 (不提供则使用今天)
   *       - in: query
   *         name: period
   *         schema:
   *           type: string
   *           enum: [day, week, month]
   *         default: week
   *         description: 分析周期
   *     responses:
   *       "200":
   *         description: 获取成功
   *       "400":
   *         description: 参数错误
   *       "401":
   *         $ref: '#/components/responses/Unauthorized'
   *       "403":
   *         $ref: '#/components/responses/Forbidden'
   */
  .get(
    auth(),
    authorize(['manageUsers', 'manageContent']),
    validate(nutritionValidation.getNutritionAnalysis),
    nutritionController.getNutritionAnalysis
  );

module.exports = router;